<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validate-trigger="submit" err-show-type="toast">
			<uni-forms-item name="type" label=""><uni-easyinput placeholder="服务类型，0:到全天服务，1:时段服务" type="number" v-model="formData.type" /></uni-forms-item>
			<uni-forms-item name="service_location" label="">
				<uni-easyinput placeholder="服务类型，0:到医院服务，1:到家服务" type="number" v-model="formData.service_location" />
			</uni-forms-item>
			<uni-forms-item name="status" label="">
				<uni-easyinput placeholder="订单状态，0:已评论（订单结束），1:已付款（待服务），2:服务中，3:服务结束，4:已取消" type="number" v-model="formData.status" />
			</uni-forms-item>
			<uni-forms-item name="user_id" label=""><uni-easyinput placeholder="用户id，参考uni-id-users表" v-model="formData.user_id" /></uni-forms-item>
			<uni-forms-item name="paramedic_id" label=""><uni-easyinput placeholder="护工id，参考uni-id-users表" v-model="formData.paramedic_id" /></uni-forms-item>
			<uni-forms-item name="level" label="">
				<uni-easyinput placeholder="订单所属服务级别，级别高的护工可以选择接受低级别的订单" type="number" v-model="formData.level" />
			</uni-forms-item>
			<uni-forms-item name="arrangement" label=""><uni-data-checkbox :multiple="true" v-model="formData.arrangement" /></uni-forms-item>
			<uni-forms-item name="patient_name" label=""><uni-easyinput placeholder="患者姓名" v-model="formData.patient_name" trim="both" /></uni-forms-item>
			<uni-forms-item name="patient_age" label=""><uni-easyinput placeholder="患者年龄" type="number" v-model="formData.patient_age" /></uni-forms-item>
			<uni-forms-item name="tel" label=""><uni-easyinput placeholder="联系电话" v-model="formData.tel" /></uni-forms-item>
			<uni-forms-item name="hospital_id" label=""><uni-easyinput placeholder="订单所在医院" v-model="formData.hospital_id" /></uni-forms-item>
			<uni-forms-item name="ward" label=""><uni-easyinput placeholder="订单所在详细地址信息" v-model="formData.ward" trim="both" /></uni-forms-item>
			<uni-forms-item name="mark" label=""><uni-easyinput placeholder="病人大致情况备注" v-model="formData.mark" trim="both" /></uni-forms-item>
			<uni-forms-item name="total_price" label=""><uni-easyinput placeholder="订单总金额" type="number" v-model="formData.total_price" /></uni-forms-item>
			<uni-forms-item name="score" label="">
				<uni-easyinput placeholder="用户评价分之：可选1，2，3，4，5，最高分5分，最低分1分" type="number" v-model="formData.score" />
			</uni-forms-item>
			<uni-forms-item name="tags" label=""><uni-data-checkbox :multiple="true" v-model="formData.tags" /></uni-forms-item>
			<uni-forms-item name="comment" label=""><uni-easyinput placeholder="用户评价内容" v-model="formData.comment" trim="both" /></uni-forms-item>
			<uni-forms-item name="arrange_day" label=""><uni-data-checkbox :multiple="true" v-model="formData.arrange_day" /></uni-forms-item>
			<uni-forms-item name="arrange_hour" label=""><uni-data-checkbox :multiple="true" v-model="formData.arrange_hour" /></uni-forms-item>
			<uni-forms-item name="day_sum" label=""><uni-easyinput placeholder="天数总和" type="number" v-model="formData.day_sum" /></uni-forms-item>
			<uni-forms-item name="hour_sum" label=""><uni-easyinput placeholder="小时总和" type="number" v-model="formData.hour_sum" /></uni-forms-item>
			<uni-forms-item name="start_hour" label=""><uni-easyinput placeholder="开始小时" v-model="formData.start_hour" /></uni-forms-item>
			<uni-forms-item name="end_hour" label=""><uni-easyinput placeholder="结束小时" v-model="formData.end_hour" /></uni-forms-item>
			<uni-forms-item name="pay_type" label=""><uni-easyinput placeholder="支付方式，0 微信支付 1 支付宝支付" type="number" v-model="formData.pay_type" /></uni-forms-item>
			<uni-forms-item name="create_time" label=""><uni-datetime-picker return-type="timestamp" :value="formData.create_time" /></uni-forms-item>
			<uni-forms-item name="update_time" label=""><uni-datetime-picker return-type="timestamp" :value="formData.update_time" /></uni-forms-item>
			<uni-forms-item name="paid_time" label=""><uni-datetime-picker return-type="timestamp" :value="formData.paid_time" /></uni-forms-item>
			<uni-forms-item name="refund_time" label=""><uni-datetime-picker return-type="timestamp" :value="formData.refund_time" /></uni-forms-item>
			<uni-forms-item name="updated_at" label=""><uni-datetime-picker return-type="timestamp" :value="formData.updated_at" /></uni-forms-item>

			<view class="uni-button-group"><button type="primary" class="uni-button" @click="submit">提交</button></view>
		</uni-forms>
	</view>
</template>

<script>
import { validator } from '../../js_sdk/validator/orders.js';

const db = uniCloud.database();
const dbCollectionName = 'orders';

function getValidator(fields) {
	let reuslt = {};
	for (let key in validator) {
		if (fields.indexOf(key) > -1) {
			reuslt[key] = validator[key];
		}
	}
	return reuslt;
}

export default {
	data() {
		return {
			formData: {
				type: null,
				service_location: null,
				status: null,
				user_id: '',
				paramedic_id: '',
				level: null,
				arrangement: [],
				patient_name: '',
				patient_age: null,
				tel: '',
				hospital_id: '',
				ward: '',
				mark: '',
				total_price: null,
				score: null,
				tags: [],
				comment: '',
				start_date: null,
				end_date: null,
				arrange_day: [],
				arrange_hour: [],
				day_sum: null,
				hour_sum: null,
				start_hour: '',
				end_hour: '',
				pay_type: null,
				platform: null,
				create_time: null,
				update_time: null,
				paid_time: null,
				refund_time: null,
				refund_desc: null,
				transaction_id: null,
				updated_at: null
			},
			formOptions: {},
			rules: {
				...getValidator([
					'type',
					'service_location',
					'status',
					'user_id',
					'paramedic_id',
					'level',
					'arrangement',
					'patient_name',
					'patient_age',
					'tel',
					'hospital_id',
					'ward',
					'mark',
					'total_price',
					'score',
					'tags',
					'comment',
					'start_date',
					'end_date',
					'arrange_day',
					'arrange_hour',
					'day_sum',
					'hour_sum',
					'start_hour',
					'end_hour',
					'pay_type',
					'platform',
					'create_time',
					'update_time',
					'paid_time',
					'refund_time',
					'refund_desc',
					'transaction_id',
					'updated_at'
				])
			}
		};
	},
	onReady() {
		this.$refs.form.setRules(this.rules);
	},
	methods: {
		/**
		 * 触发表单提交
		 */
		submit() {
			uni.showLoading({
				mask: true
			});
			this.$refs.form
				.submit()
				.then(res => {
					this.submitForm(res);
				})
				.catch(errors => {
					uni.hideLoading();
				});
		},

		submitForm(value) {
			// 使用 clientDB 提交数据
			db.collection(dbCollectionName)
				.add(value)
				.then(res => {
					uni.showToast({
						icon: 'none',
						title: '新增成功'
					});
					this.getOpenerEventChannel().emit('refreshData');
					setTimeout(() => uni.navigateBack(), 500);
				})
				.catch(err => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					});
				})
				.finally(() => {
					uni.hideLoading();
				});
		}
	}
};
</script>

<style>
.uni-container {
	padding: 15px;
}

.uni-input-border,
.uni-textarea-border {
	width: 100%;
	font-size: 14px;
	color: #666;
	border: 1px #e5e5e5 solid;
	border-radius: 5px;
	box-sizing: border-box;
}

.uni-input-border {
	padding: 0 10px;
	height: 35px;
}

.uni-textarea-border {
	padding: 10px;
	height: 80px;
}

.uni-button-group {
	margin-top: 50px;
	display: flex;
	justify-content: center;
}

.uni-button {
	width: 184px;
	padding: 12px 20px;
	font-size: 14px;
	border-radius: 4px;
	line-height: 1;
	margin: 0;
}
</style>
